package ferp.core.player;

import ferp.core.game.Game;

import java.util.LinkedList;

/**
 * User: user
 * Date: 5/20/11 7:53 PM
 */
public class Score
{
  public static class Values
  {
    public boolean changed;
    public long current;
    public LinkedList<Long> history = new LinkedList<Long>();

    public void reset()
    {
      changed = false;
      current = 0;
      history.clear();
    }

    public void add(long value)
    {
      if (value != 0)
      {
        // add to history only once
        // in Rostov current can be 0 in mountain and it must be shown
        if (!changed && (history.size() > 0 || current != 0))
          history.add(current);

        changed = true;
        current += value;
      }
    }

    // settle the score (make it look not changed)
    public void settle() {changed = false;}

    public boolean changed() {return changed;}
  }

  public static class Closable extends Values
  {
    public long delta;

    public void reset()
    {
      super.reset();

      delta = 0;
    }

    public long eventual() {return current + delta;}
  }

  public static class Balance
  {
    public long current;
    public long delta;

    public void reset()
    {
      current = delta = 0;
    }
  }

  public static class Pool extends Values {}

  // balance (the field named "balance" was in the previous version of the Score class
  // so we name the new one differently to prevent GSON errors)
  public Balance balance2 = new Balance();
  public Closable mountain = new Closable();
  public Pool pool = new Pool();
  public Closable[] whists = new Closable[Game.PLAYERS];

  public Score()
  {
    for (int i = 0; i < whists.length; ++i)
      whists[i] = new Closable();
  }

  // add score
  public void settle()
  {
    mountain.settle();
    pool.settle();

    for (Closable w : whists)
      w.settle();
  }

  public void reset()
  {
    balance2.reset();
    mountain.reset();
    pool.reset();

    for (Closable w : whists)
      w.reset();
  }

  @Override
  public String toString()
  {
    StringBuilder s = new StringBuilder();

    s.append("[M:").append(mountain.current).append(",P:").append(pool.current).append(",W:");

    for(int i = 0; i < Game.PLAYERS; i++)
      s.append(whists[i].current).append(",");

    s.setCharAt(s.length() - 1, ']');

    return s.toString();
  }
}
